import {Message, Loading} from 'element-ui'

export default function ({store, redirect, route, app:{$axios}}, inject) {
    $axios.defaults.retry = 3;
    $axios.defaults.retryDelay = 1000;
    
    // 后端地址
    //$axios.defaults.baseURL = process.env.NODE_ENV == "dev" ? "/api" : "localhost"

    // Request拦截器：比如设置token
    $axios.onRequest((config)=>{
        config.headers['Authorization'] = "Bearer " + localStorage.getItem("token")
    })

    // Error拦截器： 出现错误的时候被调用，根据状态码做对应判断显示全局Message
    $axios.onError((error) =>{
        const code = parseInt(error.response && error.response.status)
        switch(code){
            case 400:
                Message.error('request error')
                break;
            case 401:
                Message.error('token expired')
                localStorage.clear();
                break;
            case 403:
                Message.error('reject operation')
                break;
            case 404:
                Message.error(`address error:${error.response.config.url}`)
                redirect('/vuex') 
                break;
            case 500:
                Message.error('server error')
                break;
            default:
                break;
                
        }
    })
}